angular.module('app').component('winitSort', {
    templateUrl: getTemplateUrl('matrix/component/tpl/winitSort.html'),
    bindings: {
        datas: "<",
        winitId: "@",
        commitFun: '&',
        showKey: "@",//展示key
        key: "@"//唯一key
    },
    controller: function ($scope, $interval, lodash, $filter, $timeout) {
        var ctrl = this;
        if (!ctrl.winitId || ctrl.winitId == null || ctrl.winitId == '') {
            ctrl.winitId = "sortDiv";
        }
        $scope.rtnList = [];
        $scope.$watch('$viewContentLoaded', function () {
            $timeout(function () {
                $('#' + ctrl.winitId).bind('sortupdate', function (e) {
                    $scope.rtnList = [];
                    angular.forEach(e.currentTarget.children, function (child) {
                        var obj = {};
                        obj[ctrl.key] = child.attributes['data-id'].value;
                        var data = lodash.findWhere(ctrl.datas, obj);
                        $scope.rtnList.push(data);
                    })
                })
            })
        });
        $scope.click = function () {
            ctrl.commitFun({list: $scope.rtnList});
        }
    }

})